<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>x-admin</title>
    <link rel="stylesheet" href="/static/xadmin/lib/layui/css/layui.css">

    <style>

    </style>
    <script>
        /^http(s*):\/\//.test(location.href) || alert('请先部署到 localhost 下再访问');
    </script>
</head>
<body>
<div class="layui-form" enctype="multipart/form-data">
    <table class="layui-table">
        <input type="hidden" id="id" value="{$find.id}">
        <tr>
            <td>
                <label class="layui-form-label">会员名称</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <input type="text" id="user_name" value="{$find.user_name}" required  lay-verify="required" placeholder="请输入会员名称" autocomplete="off" class="layui-input">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">会员密码</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <input type="text" id="password" required value="{$find.password}"  lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">邮箱</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <input type="text" id="email" required value="{$find.email}"  lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">电话</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <input type="number" min="1" id="phone" required  value="{$find.phone}" lay-verify="required" placeholder="请输入电话" autocomplete="off" class="layui-input">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">年龄</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <input type="number" min="1" id="age" required  value="{$find.age}"  lay-verify="required" placeholder="请输入年龄" autocomplete="off" class="layui-input">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">会员头像</label>
            </td>
            <td >
                <div class="layui-input-block">
                    <div class="layui-upload">
                        <input class="img-input" type="file" value="" id="test1" data-src="{$find.user_logo}" onchange="basess(this)" />
                        <img alt class="image" id="img1" src="{$find.user_logo}" />
<!--
                        <button type="button" class="layui-btn" id="test1" data-src="{$find.user_logo}">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1" src="{$find.user_logo}" >
                            <p id="demoText"></p>
                        </div>-->
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <label class="layui-form-label">状态</label>
            </td>
            <td>
                <div class="layui-input-block">
                    <input type="radio" name="status" value="1" title="开启"  {if condition="$find.status eq 1 "}checked{/if}>
                    <input type="radio" name="status" value="2" title="关闭"  {if condition="$find.status eq 2 "}checked{/if}>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center">
                <div class="layui-input-block">
                    <button class="layui-btn" onclick="add_sumbit()">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </td>
        </tr>
    </table>
</div>
<script src="/static/xadmin/lib/layui/jquery-3.2.1.min.js"></script>
<script src="/static/xadmin/lib/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/xadmin/' //静态资源所在路径
    });
    layui.use(['laydate','form'], function(){
        var laydate = layui.laydate;
        var table = layui.table;
        var form = layui.form;
    });
//    layui.use('upload', function() {
//        var $ = layui.jquery
//            , upload = layui.upload;
//        //普通图片上传
//        var uploadInst = upload.render({
//            elem: '#test1'
//            ,url: "{:url('admin/upload/img_save',['type'=>'user'])}"
//            ,accept:'file'  // 允许上传的文件类型
//            ,auto:true // 自动上传
//            ,before: function(obj){
//                //预读本地文件示例，不支持ie8
//                obj.preview(function(index, file, result){
//                    $('#demo1').attr('src', result); //图片链接（base64）
//                });
//            }
//            ,done: function(res){
//                if(res.code==1){
//                    $('#test1').attr('data-src', res.url);  //成功返回路径存到数据库
//                    return layer.msg('上传成功',{icon:1});
//
//                }else {
//                    return layer.msg('上传失败');
//                }
//                //上传成功
//            }
//            ,error: function(){
//                //演示失败状态，并实现重传
//                var demoText = $('#demoText');
//                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
//                demoText.find('.demo-reload').on('click', function(){
//                    uploadInst.upload();
//                });
//            }
//        });
//    });
    //添加
    function add_sumbit() {
        //获取值
        var id=$("#id").val();
        var user_name=$("#user_name").val();
        var password=$("#password").val();
        var email=$("#email").val();
        var phone=$("#phone").val();
        var age=$("#age").val();
        var user_logo=$("#test1").attr("data-src");
        var status = $('input[name=status]:checked').val();
        $.post(
            "{:url('admin/user/edit')}",
            {id:id,password:password,user_name:user_name,email:email,phone:phone,status:status,age:age,user_logo:user_logo},
            function (data) {
                console.log(data);
                if(data.code==1){
                    layer.msg(data.msg,{icon:1});
                    setTimeout(function () {
                        parent.layer.closeAll();
                        parent.location.reload();
                    },1500);
                }else{
                    layer.msg(data.msg,{icon:2});
                }
            });

    }

    function basess(t) {
        var file = t.files[0];
        // 读取文件:
        var reader = new FileReader();
        reader.onload = function(e) {
            console.log(e);
            var data = e.target.result; // '...(base64编码)...'
            $(t).parent().find('img').attr('src', data);
            $(t).attr('data-src', data);

        };
        // 以DataURL的形式读取文件:
        reader.readAsDataURL(file);
    }
</script>
</body>
</html>
